<!-- ModalComponent.vue -->
<template>
	<view v-if="show" class="modal">
		<view class="modal-content">
			<view>选择房间</view>
			<picker mode="selector" :range="RoomName" @change="onRoomChange">
				<view class="picker">{{ selectedRoom || '选择房间' }}</view>
			</picker>
			<picker mode="selector" :range="filteredDeviceNames" @change="onDeviceChange" v-if="selectedRoom">
				<view>选择设备</view>
				<view class="picker">{{ selectedDeviceName || '选择设备' }}</view>
			</picker>
			<picker mode="selector" :range="statusOptions" @change="onStatusChange" v-if="selectedDevice">
				<view>选择状态</view>
				<view class="picker">{{ selectedStatus || '选择状态' }}</view>
			</picker>
			<view class="button-container">
				<button @click="closeModal">取消</button>
				<button @click="confirmSelection">确定</button>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			show: Boolean,
			RoomName: Array,
			devices: {
				type: Array,
			},
		},
		data() {
			return {
				selectedRoom: '',
				selectedDevice: '',
				selectedStatus: '',
				statusOptions: ['打开', '关闭', '自动模式'],
			};
		},
		computed: {
			filteredDevices() {
				if (!this.selectedRoom) return [];
				return this.devices.filter(device => device.roomName === this.selectedRoom);
			},
			filteredDeviceNames() {
				return this.filteredDevices.map(device => device.deviceName);
			},
		},
		methods: {
			onRoomChange(event) {
				this.selectedRoom = this.RoomName[event.detail.value];
				this.selectedDeviceName = '';
				this.selectedDevic = '';
				this.selectedStatus = '';
			},
			onDeviceChange(event) {
				this.selectedDeviceName = this.filteredDeviceNames[event.detail.value];
				this.selectedDevice = this.filteredDevices[event.detail.value];
				this.selectedStatus = '';
			},
			onStatusChange(event) {
				this.selectedStatus = this.statusOptions[event.detail.value];
			},
			confirmSelection() {
				if (!this.selectedRoom || !this.selectedDevice || !this.selectedStatus) {
					uni.showToast({
						title: '请填写完整',
						icon: 'none'
					});
					return;
				}console.log("selectedDevice",this.selectedDevice);
				this.$emit('confirm', {
					room: this.selectedRoom,
					devicename: this.selectedDevice.deviceName,
					 deviceid: this.selectedDevice.id, 
					device: this.selectedDevice,
					status: this.selectedStatus,
				});
				console.log("device:" , this.selectedDevice)
				console.log(this.selectedRoom,this.selectedDevice.deviceName,this.selectedDevice.id,this.selectedDevice.devicePic,this.selectedStatus);
				this.closeModal();
			},
			closeModal() {
				this.resetSelections();
				this.$emit('close');
			},
			resetSelections() {
				this.selectedRoom = '';
				this.selectedDeviceName = '';
				this.selectedDevice = null;
				this.selectedStatus = '';
			},
		},
	};
</script>
<style scoped>
	.modal {
		position: fixed;
		top: 0;
		left: 0;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.modal-content {
		background: #fff;
		padding: 20px;
		border-radius: 10px;
		width: 80%;
	}

	.picker {
		padding: 10px;
		margin: 10px 0;
		background-color: #f4f4f4;
		border-radius: 5px;
	}

	.button-container {
		display: flex;
		justify-content: space-between;
	}

	.button {
		margin: 5px;
		padding: 10px;
		flex: 1;
		background-color: #007bff;
		color: #fff;
		border: none;
		border-radius: 5px;
		text-align: center;
	}

	.button:last-child {
		background-color: #6c757d;
	}
</style>
